﻿//Api Class



function LMSAPI_1484_11() {
    this.LMSInitialize = LMSInitialize;
    this.LMSTerminate = LMSTerminate;
    this.LMSGetValue = LMSGetValue;

    this.LMSSetValue = LMSSetValue;
    this.LMSCommit = LMSCommit;
    this.LMSGetLastError = LMSGetLastError;

    this.LMSGetErrorString = LMSGetErrorString;
    this.LMSGetDiagnostic = LMSGetDiagnostic;
    this.LMSFinish = LMSFinish;


    this.MyData = new ScoData();

    this.err = 0;

    this.errArr = [];

    this.errArr.push ('Normal');
    this.errArr.push ('Didn\'t found the value in data');
    this.errArr.push('Server error');



    this.submitData = submitData;

    this.parentWindow = window;

    if (window.opener != undefined && window.opener != null)
      this.parentWindow = window.opener ;

    





    this.inApi = this.parentWindow.inApi;

    setTimeout(timer1, 10000);

}


function timer1() {
    setTimeout(timer1, 10000);
    try
    {
        window.API.submitData();
    }
    catch(ex) {
        
    }
}



//var myXml = null;

function submitData() {

   

    this.inApi.sendRequest (RegisteredCourses.CoursesID.toString(),this.MyData.serialize(), RegisteredCourses.ScoID.toString())

//    myXml = new ajaxRequest();

//    
//    
//    

//    try {
//        if (myXml == false) {
//            window.location = "./loginen.aspx";
//        }
//    }
//    catch (ex) {

//    }

//    myXml.onreadystatechange = function () {
//        if (myXml.readyState == 4) {
//            if (myXml.status == 200) {


//                if (myXml.responseText.indexOf("false") != -1) {
//                    //alert (myXml.responseText);
//                    // server error no login user found
//                    window.location = "./loginen.aspx";
//                }

//            }
//            else {
//                //client error , no internet connection
//                window.location = "./loginen.aspx";

//            }
//        }

//    }


//    myXml.open("POST", "./addRegCourseData.ashx", true);

//    var params = "CoursesID=" + RegisteredCourses.CoursesID.toString() + "&ScoID=" + RegisteredCourses.ScoID.toString() + 
//                 "&CurrentData=" + this.MyData.serialize();

//    //alert (params);

//    myXml.setRequestHeader("Content-type", "application/x-www-form-urlencoded")

//    myXml.send(params);


}


function LMSFinish(str) {
    
    //alert('Finishing');


    this.submitData();
    this.err = 0;

    try {
        this.parentWindow.location.reload(true);
    }
    catch (error) {
    }
    
    return true;
}


function LMSInitialize(str) 
    {
        //alert('Initializing');

        this.err = 0;

        
        return true;
    }


    function LMSTerminate(str) {
        //alert('Terminating');


        this.submitData();

        this.err = 0;

        try {
            this.parentWindow.location.reload(true);
        }
        catch (error) {
        }
        return true;
    }


    function LMSGetValue(str) {
        
        var strRet = this.MyData.get(str);
        if (strRet == null)
            strRet = '';
            
            this.err = 0;
            //alert('Getting : ' + str + ' value = ' + strRet);
        return strRet;
    }

    function LMSSetValue(prm1, prm2) {
        this.MyData.set(prm1,prm2);
        //alert('setting ' + prm1 + " : " + prm2);
        //alert(this.MyData.serialize());


        //this.submitData();
        this.err = 0;
             
        return true;
    }


    function LMSCommit(str) {

        
        this.submitData();
        this.err = 0;
        return true;
    }


    function LMSGetLastError() {
        return this.err;
    }



    function LMSGetErrorString(str) {
        try {
            return this.errArr[str];
        }
        catch (Error) {
        }

        return '';
    }



    function LMSGetDiagnostic(parameter) {

        this.err = 0;
        return '';
    }


    







    //sco Item class (key , value)
    

    function ScoDataItem(key1,value1) {
        this.key = key1;
        this.value = value1;
    }


    





    //sco data class

    function ScoData() {
        this.data = [];
        this.add = add;
        this.get = get;
        this.set = set;
        this.serialize = serialize;
        this.deserialize = deserialize;

        
    }







    function add(item) {
        
        this.data.push(item);
    }



    function get(key) {
        for (var i = 0; i < this.data.length; i++) {
            if (this.data[i].key == key) {
                
                return this.data[i].value;
            }
        }

        return null;
    }


    function set(key, value) {
        if (key == '')
            return;
        for (var i = 0; i < this.data.length; i++) {
            if (this.data[i].key == key) {
                this.data[i].value = value;
                return;
            }
        }


        this.add(new ScoDataItem(key, value));


    }


    function serialize() {

        var strRet = "";

        for (var i = 0; i < this.data.length; i++) {

            var kkey = "";
            var kvalue = "";

            try {
                kkey = this.data[i].key.toString();
                kvalue = this.data[i].value.toString();
            }
            catch (ex) {
            }

            if (kkey != '') {

                if (strRet == "") {
                           

                        strRet = kkey + ";shohdi;" + kvalue;
                    
                }
                else {
                    strRet = strRet + "|shohdi|" + kkey + ";shohdi;" + kvalue;
                }
            }
        }


        
        return strRet;

    }



    function deserialize(strValue) {
        this.data = [];

        var arr = strValue.split("|shohdi|");

        for (var i = 0; i < arr.length; i++) {
            var nArr = arr[i].split(";shohdi;");

            var item = new ScoDataItem(nArr[0], nArr[1]);
            this.add(item);
        }
       

    }